package com.xiaomaoguai.common;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * 屏幕截图
 *
 * @ ClassName: CaptureScreen.java
 * @ Author: WeiHui-Z
 * @ Date: 2016/12/29 15:55
 * @ Version: v1.0.0
 */
public class CaptureScreen {

	public static void captureScreen(String fileName) {
		try {
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			Rectangle screenRectangle = new Rectangle(screenSize);
			Robot robot = new Robot();
			BufferedImage image = robot.createScreenCapture(screenRectangle);
			File imgFile = new File(fileName);
			ImageIO.write(image, "png", imgFile);
			if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN))
				Desktop.getDesktop().open(imgFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		captureScreen("F:\\Test22.jpg");
	}
}
